<?php
defined('IN_IA') or exit('Access Denied');
require IA_ROOT. '/addons/ew_delivery/defines.php';
require EW_DELIVERT_INC.'function.php';
class ew_deliveryModuleSite extends WeModuleSite {
	public function __construct(){
		global $_W;
		//判断用户信息是否存在，不存在添加
        //这里做一下授权
        $url = $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

        \Yii::info('$url is'.$url);
        \Yii::info('$_W is'.json_encode($_W));
	}

    /*
    public function getFansDBInfo($openid)
    {
        global $_W;
        $fans = pdo_fetch('SELECT * FROM ' . tablename($this->fanstable) . " WHERE uniacid = '{$_W['uniacid']}' and openid ='{$openid}'");
        return $fans;
    }

    public function authUser()
    {
        global $_GPC, $_W;
        if (!isset($_GPC['code'])) {
            $baseUrl = urlencode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']);
            $url = $this->__CreateOauthUrlForCode($baseUrl, 'snsapi_userinfo');
            Header("Location: {$url}");
            die;
        } else {
            $code = $_GPC['code'];
            $base = $this->getBaseFromMp($code);
            $user = $this->getUserInfoFromMp($base);
            return $user;
        }
    }

    private function ToUrlParams($urlObj)
    {
        $buff = '';
        foreach ($urlObj as $k => $v) {
            if ($k != 'sign') {
                $buff .= $k . '=' . $v . '&';
            }
        }
        $buff = trim($buff, '&');
        return $buff;
    }

    private function __CreateOauthUrlForUserInfo($base)
    {
        global $_W;
        $urlObj['access_token'] = $base['access_token'];
        $urlObj['openid'] = $base['openid'];
        $bizString = $this->ToUrlParams($urlObj);
        return 'https://api.weixin.qq.com/sns/userinfo?' . $bizString;
    }

    public function getUserInfoFromMp($base)
    {
        $url = $this->__CreateOauthUrlForUserInfo($base);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $res = curl_exec($ch);
        curl_close($ch);
        $data = json_decode($res, true);
        return $data;
    }

    public function getBaseFromMp($code)
    {
        $url = $this->__CreateOauthUrlForOpenid($code);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $res = curl_exec($ch);
        curl_close($ch);
        $data = json_decode($res, true);
        $base = array();
        $base['openid'] = $data['openid'];
        $base['access_token'] = $data['access_token'];
        return $base;
    }

    private function __CreateOauthUrlForCode($redirectUrl, $scope)
    {
        global $_W;
        $system = $this->module['config'];
        $urlObj['appid'] = $system['appid'];
        $urlObj['redirect_uri'] = "{$redirectUrl}";
        $urlObj['response_type'] = 'code';
        $urlObj['scope'] = "{$scope}";
        $urlObj['state'] = 'STATE' . '#wechat_redirect';
        $bizString = $this->ToUrlParams($urlObj);
        return 'https://open.weixin.qq.com/connect/oauth2/authorize?' . $bizString;
    }

    private function addFans($info)
    {
        global $_W;
        $data = array();
        if (!empty($info['nickname']) && !empty($info['openid'])) {
            $data = array('uniacid' => $_W['uniacid'], 'openid' => $info['openid'], 'nickname' => $info['nickname'], 'headimgurl' => $info['headimgurl'], 'addtime' => date('Y-m-d H:i:s'));
            pdo_insert($this->fanstable, $data);
            $fansid = pdo_insertid();
        }
        return $data;
    }

    public function checkinfo()
    {
        global $_GPC, $_W;
        $system = $this->module['config'];
        if ($system['auth'] == 0) {
            $openid = $_W['fans']['from_user'];
            $fans = $this->getFansDBInfo($openid);
            if (empty($fans)) {
                $authuser = $this->authUser();
                $fans = $this->addFans($authuser);
            }
        } else {
            $openid = $_SESSION['water_o2o_authpenid'];
            if (empty($openid)) {
                $authuser = $this->authUser();
                $openid = $authuser['openid'];
                $fans = $this->getFansDBInfo($openid);
                if (empty($fans)) {
                    $fans = $this->addFans($authuser);
                }
                $_SESSION['water_o2o_authpenid'] = $fans['openid'];
            } else {
                $fans = $this->getFansDBInfo($openid);
            }
        }
        return $fans;
    }
    */
}